﻿#include "FormulaEditorDialog.h"
#include "ui_FormulaEditorDialog.h"
#include <QIcon>
#include <QLabel.h>
#include <QVBoxLayout>
#include <QComboBox.h>
#include <QScrollArea.h>
#include <QListView.h>
#include <QStandardItemModel.h>
#include "BaseWidget/IdaWidgetHelper.h"
#include "Curve2DKeySelcetBar.h"
#include "BaseWidget/IdaDialogMsg.h"

namespace pst
{
    FormulaEditorDialog::FormulaEditorDialog(QWidget* parent)
        : IdaDialogFrame(parent), m_ui(new Ui::FormulaEditorDialog())
    {
        m_ui->setupUi(this);
        setWindowTitle(QStringLiteral("公式编辑器"));
        setWindowIcon(QIcon(":/include/Image/Moon.jpg"));
        // this->setIsFixedSize(true);
        setContentsMargins(0, 0, 0, 0);
        // this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::ApplicationModal);
        // setWindowModality(Qt::ApplicationModal);//设置为模态，点击其他控件不响应
        // setWindowButtonFlags(ElaAppBarType::CloseButtonHint);

        m_ui->textEdit_Expression->setStyleSheet("QTextEdit { border: none; }");
        updateUI();
    }

    FormulaEditorDialog::~FormulaEditorDialog()
    {
        delete m_ui;
    }

    void FormulaEditorDialog::updateUI()
    {

        QStringList _SelectionCurve{
            QStringLiteral("选择曲线"),
        };
        QStringList _callingFormula{
            QStringLiteral("调用公式"),
            "AVERAGE",
            "MAX",
            "MIN",
            "ABS",
            "SQRT",
            "EXP",
            "LN",
            "LOG",
            "SIN",
            "COS",
            "TAN",
            "ASIN",
            "ACOS",
            "ATAN",

        };
        QStringList _CallConstant{
            QStringLiteral("调用常量"),
            "pi",
            "eps0",
            "mu0",
            "z0",
            "c0",
        };
        m_ui->comboBox_SelectionCurve->setView(new QListView());
        m_ui->comboBox_CallingFormula->setView(new QListView());
        m_ui->comboBox_CallConstant->setView(new QListView());

        m_ui->comboBox_SelectionCurve->addItems(_SelectionCurve);
        m_ui->comboBox_CallingFormula->addItems(_callingFormula);
        m_ui->comboBox_CallConstant->addItems(_CallConstant);

        QListView* view1 = dynamic_cast<QListView*>(m_ui->comboBox_SelectionCurve->view());
        if (view1 != nullptr)
        {
            view1->setRowHidden(0, true);
        }
        QListView* view2 = dynamic_cast<QListView*>(m_ui->comboBox_CallingFormula->view());
        if (view2 != nullptr)
        {
            view2->setRowHidden(0, true);
        }
        QListView* view3 = dynamic_cast<QListView*>(m_ui->comboBox_CallConstant->view());
        if (view3 != nullptr)
        {
            view3->setRowHidden(0, true);
        }

        IdaWidgetHelperInstance->setMainButtonStyle(m_ui->pushButton_accept);
        connect(m_ui->pushButton_reject, &QPushButton::clicked, this, &FormulaEditorDialog::reject);
        connect(m_ui->pushButton_accept, &QPushButton::clicked, this, &FormulaEditorDialog::accept);
    }

    void FormulaEditorDialog::setCurveList(QStringList curveList)
    {
        m_curveList = curveList;
        m_ui->comboBox_SelectionCurve->addItems(m_curveList);
    }

    void FormulaEditorDialog::reject()
    {
        IdaDialogFrame::reject();
    }

    void FormulaEditorDialog::accept()
    {
        m_formulaContent = m_ui->textEdit_Expression->toPlainText();
        bool isSuccess = false;
        emit signal_calculationCheck(m_formulaContent, isSuccess);
        if (isSuccess)
        {
            IdaDialogFrame::accept();
        }
        else
        {
            EDialogMsgResult _result = IdaDialogMsg::showDialog(this, "公式错误", EDialogMsg::Error, tr("自由编辑错误"));
        }
    }

    void FormulaEditorDialog::on_comboBox_SelectionCurve_currentIndexChanged(int index)
    {
        // m_curveIndex = m_ui->comboBox_SelectionCurve->currentIndex();
        m_currentFormula = m_ui->comboBox_SelectionCurve->currentText();
        if (m_currentFormula != "选择曲线")
            m_ui->textEdit_Expression->insertPlainText(m_currentFormula);
        m_ui->comboBox_SelectionCurve->setCurrentText(QStringLiteral("选择曲线"));
        m_ui->textEdit_Expression->setFocus();
    }
    void FormulaEditorDialog::on_comboBox_CallingFormula_currentIndexChanged(const QString& content)
    {
        m_currentFormula = m_ui->comboBox_CallingFormula->currentText();
        if (m_currentFormula != "调用公式")
        {
            m_ui->textEdit_Expression->insertPlainText(m_currentFormula + "()");
            m_ui->textEdit_Expression->moveCursor(QTextCursor::Left);
        }
        m_ui->comboBox_CallingFormula->setCurrentText(QStringLiteral("调用公式"));
        m_ui->textEdit_Expression->setFocus();
    }
    void FormulaEditorDialog::on_comboBox_CallConstant_currentIndexChanged(const QString& content)
    {
        m_currentConstant = m_ui->comboBox_CallConstant->currentText();
        if (m_currentConstant != "调用常量")
            m_ui->textEdit_Expression->insertPlainText(m_currentConstant);
        m_ui->comboBox_CallConstant->setCurrentText(QStringLiteral("调用常量"));
        m_ui->textEdit_Expression->setFocus();
    }

    QString FormulaEditorDialog::getFormulaContent()
    {
        return m_formulaContent;
    }
    void FormulaEditorDialog::setFormulaContent(const QString& formulaContent)
    {
        m_formulaContent = formulaContent;
        m_ui->textEdit_Expression->setPlainText(m_formulaContent);
    }
} // namespace pst
